package com.sale.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sale.common.core.exception.GlobalException;
import com.sale.system.domain.SysTemplateDisposition;
import com.sale.system.domain.dto.SysTemplateDispositionDto;
import com.sale.system.domain.vo.AddDispositionVo;
import com.sale.system.domain.vo.SysTemplateDispositionVo;
import com.sale.system.mapper.SysTemplateDispositionMapper;
import com.sale.system.service.SysTemplateDispositionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 打印配置数据表实现层
 */
@RequiredArgsConstructor
@Service
public class SysTemplateDispositionServiceImpl implements SysTemplateDispositionService {

    private final SysTemplateDispositionMapper sysTemplateDispositionMapper;

    /**
     * 新增配置
     *
     * @param addDispositionVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDisposition(AddDispositionVo addDispositionVo) {
        List<SysTemplateDispositionVo> dispositionList = addDispositionVo.getDispositionList();
        List<SysTemplateDisposition> dispositions = new ArrayList<>();
        //通过菜单id获取配置
        List<SysTemplateDisposition> dispositions1 = sysTemplateDispositionMapper.selectList(new QueryWrapper<SysTemplateDisposition>().lambda().eq(SysTemplateDisposition::getMenuId, addDispositionVo.getMenuId()));
        if (dispositions1.size() > 0) {
            //删除之前的数据
            sysTemplateDispositionMapper.deleteBatchIds(dispositions1);
            //添加新的数据
            for (SysTemplateDispositionVo entity : dispositionList) {
                SysTemplateDisposition sysTemplateDisposition = new SysTemplateDisposition();
                sysTemplateDisposition.setMenuId(addDispositionVo.getMenuId())
                    .setDefaultValue(entity.getDefaultValue())
                    .setPrintName(entity.getPrintName())
                    .setPrintField(entity.getPrintField())
                    .setPrintType(entity.getPrintType())
                    .setIcon(entity.getIcon())
                    .setCreateTime(new Date());
                dispositions.add(sysTemplateDisposition);
            }
            return sysTemplateDispositionMapper.insertBatch(dispositions);
        }else {
            if (addDispositionVo.getDispositionList() == null) {
                throw new GlobalException("至少添加一条配置");
            }
            for (SysTemplateDispositionVo entity : dispositionList) {
                SysTemplateDisposition sysTemplateDisposition = new SysTemplateDisposition();
                sysTemplateDisposition.setMenuId(addDispositionVo.getMenuId())
                    .setDefaultValue(entity.getDefaultValue())
                    .setPrintName(entity.getPrintName())
                    .setPrintField(entity.getPrintField())
                    .setPrintType(entity.getPrintType())
                    .setIcon(entity.getIcon())
                    .setCreateTime(new Date());
                dispositions.add(sysTemplateDisposition);
            }
            return sysTemplateDispositionMapper.insertBatch(dispositions);
        }
    }

    /**
     * 通过菜单id获取配置信息
     *
     * @param menuId
     * @return
     */
    @Override
    public List<SysTemplateDispositionDto> getDataByMenuId(Long menuId) {

        return sysTemplateDispositionMapper.getTemplateDisposition(menuId);
    }
}
